package com.wiki.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wiki.entity.Doc;
import com.wiki.service.ContentService;
import com.wiki.service.DocService;
import com.wiki.utils.CopyUtil;
import com.wiki.utils.String2List;
import com.wiki.utils.commonRequest.doc.DocQueryReq;
import com.wiki.utils.commonRequest.doc.DocSaveReq;
import com.wiki.utils.commonResponse.CommonResp;
import com.wiki.utils.commonResponse.DocQueryResp;
import com.wiki.utils.commonResponse.PageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 文档 前端控制器
 * </p>
 *
 * @author 偷闰土的猹
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private DocService docService;

    @GetMapping("/list")
    public CommonResp list(@Valid DocQueryReq req) {
        CommonResp<PageResp<DocQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<DocQueryResp> name = docService.listAll(req);
        listCommonResp.setContent(name);
        return listCommonResp;
    }

    @GetMapping("/list_by_name")
    public CommonResp list_by_name(@Valid DocQueryReq req) {
        CommonResp<PageResp<DocQueryResp>> listCommonResp = new CommonResp<>();
        PageResp<DocQueryResp> resp = docService.queryLike(req);
        listCommonResp.setContent(resp);
        return listCommonResp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody DocSaveReq req) {
        CommonResp resp = new CommonResp();
        docService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{ids}")
    public CommonResp delete(@PathVariable String ids) {
        CommonResp resp = new CommonResp();
        docService.delete(String2List.String2Array(ids));
        return resp;
    }

    @GetMapping("/all/{ebookId}")
    public CommonResp all(@PathVariable Long ebookId) {
        System.out.println("进入了all");
        CommonResp<List<DocQueryResp>> resp = new CommonResp<>();
        List<DocQueryResp> all = docService.all(ebookId);
        resp.setContent(all);
        System.out.println(resp.toString());
        return resp;
    }

    @GetMapping("/find-content/{id}")
    public CommonResp findContent(@PathVariable Long id){
        CommonResp<String> resp = new CommonResp<>();

        String content = docService.findContent(id);

        resp.setContent(content);

        return resp;
    }


}
